package com.chexiehui.jc.utils;

import android.content.Context;
import android.widget.ImageView;

import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.widget.GlideRoundTransform;
import com.chexiehui.jc.widget.RoundedCornersTransform;


import java.util.ArrayList;

/**
 * Author: SongJun.
 * CreateDate: 2019/3/1 0001.
 * Description:
 * Glide特点
 * 使用简单
 * 可配置度高，自适应程度高
 * 支持常见图片格式 Jpg png gif webp
 * 支持多种数据源  网络、本地、资源、Assets 等
 * 高效缓存策略    支持Memory和Disk图片缓存 默认Bitmap格式采用RGB_565内存使用至少减少一半
 * 生命周期集成   根据Activity/Fragment生命周期自动管理请求
 * 高效处理Bitmap  使用Bitmap Pool使Bitmap复用，主动调用recycle回收需要回收的Bitmap，减小系统回收压力
 * 这里默认支持Context，Glide支持Context,Activity,Fragment，FragmentActivity
 */
public class GlideUtil {

    public static String getUrl(String path) {
        return path == null || path.trim().equals("") ? Constants.IMAGE_SERVER_RUL :
                path.startsWith("http") ? path :
                        path.startsWith("/storage") ? path :
                                path.startsWith("/profile") ? Constants.IMAGE_SERVER_RUL + path : path;
    }


    private static String getFileExp(String fileName) {
        if (StringUtils.isTrimEmpty(fileName)) {
            return null;
        }
        if (fileName.contains(".")) {
            return fileName.substring(fileName.lastIndexOf("\\.") + 1);
        }
        return null;
    }

    public static boolean isServerImage(String path) {
        return path != null && !path.trim().equals("") && (path.startsWith("/upload") || path.startsWith("/profile"));
    }

    public static String getSplitFristUrl(String path) {
        return path == null || path.trim().equals("") ? "" :
                path.contains("|") ? path.split("\\|")[0] : path;
    }


    public static String[] getSplitUrl(String path) {
        return path == null || path.trim().equals("") ? new String[]{} :
                path.contains("|") ? path.split("\\|") : new String[]{path};
    }

    public static ArrayList<String> getSplitUrlList(String path) {
        ArrayList<String> list = new ArrayList<>();
        if (path == null || path.trim().equals("")) {
            return list;
        }
        String[] arr = path.contains("|") ? path.split("\\|") : new String[]{path};
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null && !arr[i].trim().equals("")) {
                list.add(arr[i]);
            }

        }
        return list;
    }

    /**
     * 默认加载
     *
     * @param mContext
     * @param path
     * @param mImageView
     */
    public static void loadImageView(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(getUrl(path)).into(mImageView);
    }

    /**
     * 默认加载
     *
     * @param mContext
     * @param drawable
     * @param mImageView
     */
    public static void loadImageView(Context mContext, int drawable, ImageView mImageView) {
        Glide.with(mContext).load(drawable).into(mImageView);
    }

    /**
     * 加载指定大小
     *
     * @param mContext
     * @param path
     * @param width
     * @param height
     * @param mImageView
     */
    public static void loadImageViewSize(Context mContext, String path, int width, int height, ImageView mImageView) {
        Glide.with(mContext).load(getUrl(path)).override(width, height).into(mImageView);
    }

    /**
     * 设置加载中以及加载失败图片
     *
     * @param mContext
     * @param path
     * @param mImageView
     * @param lodingImage
     * @param errorImageView
     */
    public static void loadImageViewLoding(Context mContext, String path, ImageView mImageView, int lodingImage, int errorImageView) {
        Glide.with(mContext).load(getUrl(path)).placeholder(lodingImage).error(errorImageView).into(mImageView);
    }

    /**
     * 设置加载中以及加载失败图片并且指定大小
     *
     * @param mContext
     * @param path
     * @param width
     * @param height
     * @param mImageView
     * @param lodingImage
     * @param errorImageView
     */
    public static void loadImageViewLodingSize(Context mContext, String path, int width, int height, ImageView mImageView, int lodingImage, int errorImageView) {
        Glide.with(mContext).load(getUrl(path)).override(width, height).placeholder(lodingImage).error(errorImageView).into(mImageView);
    }

    /**
     * 设置跳过内存缓存
     *
     * @param mContext
     * @param path
     * @param mImageView
     */
    public static void loadImageViewCache(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(getUrl(path)).skipMemoryCache(true).into(mImageView);
    }

    //设置下载优先级
    public static void loadImageViewPriority(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(getUrl(path)).priority(Priority.NORMAL).into(mImageView);
    }

    /**
     * 策略解说：设置缓存策略
     * <p>
     * all:缓存源资源和转换后的资源
     * <p>
     * none:不作任何磁盘缓存
     * <p>
     * source:缓存源资源
     * <p>
     * result：缓存转换后的资源
     */
    public static void loadImageViewDiskCache(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(getUrl(path)).diskCacheStrategy(DiskCacheStrategy.ALL).into(mImageView);
    }

    /**
     * 设置缩略图支持
     * 会先加载缩略图
     */
    public static void loadImageViewThumbnail(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(getUrl(path)).thumbnail(0.1f).into(mImageView);
    }

    /**
     * * api提供了比如：centerCrop()、fitCenter()等
     * 设置动态转换
     *
     * @param mContext
     * @param path
     * @param mImageView
     */
    public static void loadImageViewCrop(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(getUrl(path))
                .dontAnimate()
                .centerCrop().into(mImageView);
    }

    public static void loadImageViewCrop(Context mContext, String path, ImageView mImageView, int placeholder, int error) {
        Glide.with(mContext).load(getUrl(path))
                .dontAnimate()
                .centerCrop().placeholder(placeholder).error(error).into(mImageView);
    }


    public static void loadImageViewCircleCrop(Context mContext, String path, ImageView mImageView, int placeholder, int error) {
        Glide.with(mContext).load(getUrl(path))
                .centerCrop().placeholder(placeholder).error(error)
                .circleCrop()
                .dontAnimate()
                .into(mImageView);
    }

    public static void loadImageViewRoundCrop(Context mContext, String path, int roundDp, ImageView mImageView, int placeholder, int error) {
        RequestOptions myOptions = new RequestOptions()
                .transform(new GlideRoundTransform(mContext, roundDp));
        Glide.with(mContext).load(getUrl(path))
                .dontAnimate()
                .centerCrop().placeholder(placeholder).error(error)
                .transform(new GlideRoundTransform(mContext, roundDp))
                .into(mImageView);
    }

    public static void loadImageViewCustomerRoundCrop(Context mContext, String path, ImageView mImageView,
                                                      int placeholder, int error,
                                                      boolean leftTop, boolean rightTop, boolean leftBottom, boolean rightBottom,
                                                      int rundDp) {
        RoundedCornersTransform transform = new RoundedCornersTransform(mContext, rundDp);
        transform.setNeedCorner(leftTop, rightTop, leftBottom, rightBottom);
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(placeholder)
                .error(error)
                .transform(new CenterCrop(),transform);
        Glide.with(mContext).load(getUrl(path))
                .centerCrop()
                .apply(options)
                .placeholder(placeholder)
                .error(error).into(mImageView);
        /*
        //组合各种Transformation,
        MultiTransformation<Bitmap> mation = new MultiTransformation<>
                //Glide设置圆角图片后设置ImageVIew的scanType="centerCrop"无效解决办法,将new CenterCrop()添加至此
                (new CenterCrop(),
                        leftTop ? new RoundedCornersTransformation
                        (rundDp, 0, RoundedCornersTransformation.CornerType.TOP_LEFT) : null,
                        rightTop ? new RoundedCornersTransformation
                                (rundDp, 0, RoundedCornersTransformation.CornerType.TOP_RIGHT) : null,
                        leftBottom ? new RoundedCornersTransformation
                                (rundDp, 0, RoundedCornersTransformation.CornerType.BOTTOM_LEFT) : null,
                        rightBottom ? new RoundedCornersTransformation
                                (rundDp, 0, RoundedCornersTransformation.CornerType.BOTTOM_RIGHT) : null
                );
        Glide.with(mContext).load(getUrl(path))
                .centerCrop()
                .placeholder(placeholder)
                .error(error)
                .apply(RequestOptions.bitmapTransform(mation)).into(mImageView);*/
    }

    /*public static void loadImageViewTransformCrop(Context mContext, int source, int dp, ImageView mImageView, int placeholder, int error) {
        Glide.with(mContext).load(source)
                .centerCrop().placeholder(placeholder).error(error)
                .transform(new GlideRoundTransform(mContext, dp))
                .into(mImageView);
    }

    public static void loadImageViewCircleCrop(Context mContext, int source, ImageView mImageView, int placeholder, int error) {
        Glide.with(mContext).load(source)
                .centerCrop().placeholder(placeholder).error(error)
                .transform(new GlideRoundTransform(mContext))
                .into(mImageView);
    }

    public static void loadImageViewRoundCrop(Context mContext, String path, int roundDp, ImageView mImageView, int placeholder, int error) {
        Glide.with(mContext).load(getUrl(path))
                .centerCrop().placeholder(placeholder).error(error)
                .transform(new GlideRoundTransform(mContext, roundDp))
                .into(mImageView);
    }*/

    /**
     * 设置动态GIF加载方式
     * @param mContext
     * @param path
     * @param mImageView
     */
   /* public static void loadImageViewDynamicGif(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(getUrl(path)).asGif().into(mImageView);
    }*/

    /**
     * 设置静态GIF加载方式
     * @param mContext
     * @param path
     * @param mImageView
     */
    /*public static void loadImageViewStaticGif(Context mContext, String path, ImageView mImageView) {
        Glide.with(mContext).load(getUrl(path)).asBitmap().into(mImageView);
    }*/

    /**
     * 设置监听的用处 可以用于监控请求发生错误来源，以及图片来源 是内存还是磁盘
     * 设置监听请求接口
     * @param mContext
     * @param path
     * @param mImageView
     * @param requstlistener
     */
    /*public static void loadImageViewListener(Context mContext, String path, ImageView mImageView, RequestListener<String, GlideDrawable> requstlistener) {
        Glide.with(mContext).load(getUrl(path)).listener(requstlistener).into(mImageView);
    }*/

    /**
     * 项目中有很多需要先下载图片然后再做一些合成的功能，比如项目中出现的图文混排
     * 设置要加载的内容
     * @param mContext
     * @param path
     * @param simpleTarget
     */
    /*public static void loadImageViewContent(Context mContext, String path, SimpleTarget<GlideDrawable> simpleTarget) {
        Glide.with(mContext).load(getUrl(path)).centerCrop().into(simpleTarget);
    }*/

    /**
     * 清理磁盘缓存
     *
     * @param mContext
     */
    public static void GuideClearDiskCache(Context mContext) {
        //理磁盘缓存 需要在子线程中执行
        Glide.get(mContext).clearDiskCache();
    }

    /**
     * 清理内存缓存
     *
     * @param mContext
     */
    public static void GuideClearMemory(Context mContext) {
        //清理内存缓存  可以在UI主线程中进行
        Glide.get(mContext).clearMemory();
    }

}
